package com.lzp.badminton.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzp.badminton.config.ResultMessage;
import com.lzp.badminton.service.IClubmemberService;
import com.lzp.badminton.vo.ActivityClubJoin;
import com.lzp.badminton.vo.ClubUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2022-02-11
 */
@RestController
@RequestMapping("/clubmember")
public class ClubmemberController {
    @Autowired
    private ResultMessage resultMessage;
    @Autowired
    private IClubmemberService iClubmemberService;

    @GetMapping(value = "/getClubMember")
    @ResponseBody
    public ResultMessage getActivityInfo(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "clubId") int clubId) {
        Page<ClubUser> iPage = iClubmemberService.getClubMemberById(new Page<ClubUser>(pageNum, pageSize), clubId);

        if (iPage.getRecords().size() != 0) {
            Map result = new HashMap();
            result.put("clubMemberinfo", iPage);
            resultMessage.success("200", "获取成功", result);
            return resultMessage;
        } else {
            resultMessage.fail("404", "没有更多数据");
            return resultMessage;
        }
    }

    @GetMapping(value = "/getClubUserIn")
    @ResponseBody
    public ResultMessage getClubUserIn(@RequestParam(value = "clubId") int clubId, @RequestParam(value = "userId") String userId) {

        int result = iClubmemberService.getClubUserIn(clubId, userId);

        if (result == 0) {
            resultMessage.fail("404", "该用户没有加入俱乐部");
        } else if (result == 1) {
            resultMessage.success("200", "该用户加入了俱乐部");
        }
        return  resultMessage;

    }

    @GetMapping(value = "/userToClub")
    @ResponseBody
    public ResultMessage userToClub(@RequestParam(value = "clubId") int clubId, @RequestParam(value = "userId") String userId) {

        int result = iClubmemberService.userToClub(clubId, userId);

        if (result == 0) {
            resultMessage.fail("404", "加入俱乐部失败");
        } else if (result == 1) {
            resultMessage.success("200", "加入俱乐部成功");
        }
        return  resultMessage;

    }

    @GetMapping(value = "/userLeaveClub")
    @ResponseBody
    public ResultMessage userLeaveClub(@RequestParam(value = "clubId") int clubId, @RequestParam(value = "userId") String userId) {

        int result = iClubmemberService.userLeaveClub(clubId, userId);

        if (result == 0) {
            resultMessage.fail("404", "退出俱乐部失败");
        } else if (result == 1) {
            resultMessage.success("200", "退出俱乐部成功");
        }
        return  resultMessage;
    }

    @GetMapping(value = "/getClubMemberInfo")
    @ResponseBody
    public ResultMessage getClubMemberInfo(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", required = false, defaultValue = "5000") Integer pageSize,
                                           @RequestParam(value = "clubId") int clubId,
                                           @RequestParam(value = "nickname") String nickname) {

        Page<ClubUser> iPage = iClubmemberService.getClubMemberInfo(new Page<ClubUser>(pageNum, pageSize), clubId, nickname);

        //List<ClubUser> clubUserList = iClubmemberService.getClubMemberInfo(clubId, nickname);

        if (iPage.getRecords().size() == 0) {
            resultMessage.success("200", "该俱乐部没有成员", null);
        } else {
            resultMessage.success("200","查询俱乐部成员成功", iPage);
        }
        return  resultMessage;

    }

}
